package sdk.webview.fmc.com.fmcsdk.imagepicker.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.content.FileProvider;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.chrisbanes.photoview.PhotoView;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.List;

import sdk.webview.fmc.com.fmcsdk.R;
import sdk.webview.fmc.com.fmcsdk.imagepicker.bean.ItemType;
import sdk.webview.fmc.com.fmcsdk.imagepicker.bean.MediaFile;
import sdk.webview.fmc.com.fmcsdk.imagepicker.manager.ConfigManager;
import sdk.webview.fmc.com.fmcsdk.imagepicker.manager.ImagePickerProvider;
import sdk.webview.fmc.com.fmcsdk.imagepicker.view.SquareImageView;
import sdk.webview.fmc.com.fmcsdk.imagepicker.view.SquareRelativeLayout;

public class ImagePreDaActivity extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<MediaFile> mMediaFileList;

    public ImagePreDaActivity(Context context, List<MediaFile> mediaFileList) {
        this.mContext = context;
        this.mMediaFileList = mediaFileList;
    }

    @Override
    public int getItemViewType(int position) {
        if (mMediaFileList.get(position).getDuration() > 0) { //视频
            return ItemType.ITEM_TYPE_VIDEO;
        } else {
            return ItemType.ITEM_TYPE_IMAGE;
        }
    }

    @NonNull
    @NotNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {

        // 根据item类型创建不同的ViewHolder
        if (viewType == ItemType.ITEM_TYPE_IMAGE) {
            View view = LayoutInflater.from(mContext)
                    .inflate(R.layout.item_viewpager_image, parent, false);
            return new ImageHolder(view);
        } else if (viewType == ItemType.ITEM_TYPE_VIDEO) {
            View view = LayoutInflater.from(mContext)
                    .inflate(R.layout.item_viewpager_video, parent, false);
            return new VideoHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull RecyclerView.ViewHolder viewHolder, int position) {
        // 绑定数据到ViewHolder
        int itemType = getItemViewType(position);
        final MediaFile mediaFile = mMediaFileList.get(position);
        switch (itemType) {
            case ItemType.ITEM_TYPE_IMAGE: //图片
                try {
                    ConfigManager.getInstance().getImageLoader().loadPreImage(((ImageHolder) viewHolder).iv_item_image,
                            mediaFile.getPath());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case ItemType.ITEM_TYPE_VIDEO:  //、视频Item
                ((VideoHolder) viewHolder).iv_item_play.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //实现播放视频的跳转逻辑(调用原生视频播放器)
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        Uri uri = FileProvider.getUriForFile(mContext,
                                ImagePickerProvider.getFileProviderName(mContext), new File(mediaFile.getPath()));
                        intent.setDataAndType(uri, "video/*");
                        //给所有符合跳转条件的应用授权
                        List<ResolveInfo> resInfoList = mContext.getPackageManager()
                                .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
                        for (ResolveInfo resolveInfo : resInfoList) {
                            String packageName = resolveInfo.activityInfo.packageName;
                            mContext.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION
                                    | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                        }
                        mContext.startActivity(intent);
                    }
                });
                try {
                    ConfigManager.getInstance().getImageLoader().loadPreImage(((VideoHolder) viewHolder).iv_item_play,
                            mediaFile.getPath());
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
            //相机Item
            default:
                break;
        }
    }

    @Override
    public int getItemCount() {
        return mMediaFileList.size();
    }

    class ImageHolder extends RecyclerView.ViewHolder {

        public PhotoView iv_item_image;

        public ImageHolder(View itemView) {
            super(itemView);
            iv_item_image = itemView.findViewById(R.id.iv_item_image);
        }
    }


    class VideoHolder extends RecyclerView.ViewHolder {

        public ImageView iv_item_image, iv_item_play;

        public VideoHolder(View itemView) {
            super(itemView);
            iv_item_image = itemView.findViewById(R.id.iv_item_image);
            iv_item_play = itemView.findViewById(R.id.iv_item_play);
        }
    }
}
